<?php
require_once('../config/auth.php');
require_once('guard.cl.php');
$__CURRENT_PAGE__ = 'affect_garde';
$__TITLE__ = "Gestion des gardes";

$guard = new Guard();
$guard->generateGuards();

$serviceId = empty($_GET['service_id']) ? 0 : $_GET['service_id'];
$startDate = empty($_GET['start_date']) ? date("Y").'-01-01' : $_GET['start_date'];
$endDate = empty($_GET['end_date']) ? date("Y").'-12-31' : $_GET['end_date'];
$type = empty($_GET['type']) ? '' : $_GET['type'];
$guards = $guard->getGuardByServiceAndTypeAndDate($serviceId, $type, array('start_date'=>$startDate, 'end_date'=>$endDate));

$employe = $guard->getEmploye();
$employes = $employe->getEmployesByServiceIdAndType($serviceId, $type);
$departements = $employe->getService()->getDepartement()->getAllDepartements();

if($serviceId > 0) {
  $employe->getService()->loadServiceById($_GET['service_id']);
  $services = $employe->getService()->getServiceByDepartementId($employe->getService()->getDepartementId());
} else {
  $services = $employe->getService()->getAllServices();
}

require_once(dirname(__FILE__).'/../config/head.php');?>
<body>
<?php require_once(dirname(__FILE__).'/../config/menu.php'); ?>
<div class="container garde">

<h1>Gerer les gardes</h1>

    <div class="alert alert-success">
        <strong>Affectation des garde automatique terminée avec succès</strong>
        <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>

<form method='get'>
    <select name="departement_id" id="departement_id">
          <option value="">--Selectionner un departement--</option>
          <?php foreach($departements as $depart): ?>
            <option <?php echo ($employe->getService()->getDepartementId() == $depart->id)  ? 'selected="selected"' : '' ?>
            value="<?php echo $depart->id?>"><?php echo $depart->name ?></option>   
          <?php endforeach ?>
  </select>
  <select name="service_id" id="service_id">
          <option value="">--Selectionner un service--</option>
          <?php foreach($services as $serv): ?>
            <option <?php echo ($employe->getService()->getId() == $serv->id)  ? 'selected="selected"' : '' ?>
            value="<?php echo $serv->id?>"><?php echo $serv->name ?></option>   
          <?php endforeach ?>
  </select>
  <select name="type" id="type">
          <option value="">--Selectionner un type--</option>
          <option <?php echo ($employe->getType()==Employe::ENGINEER)  ? 'selected="selected"' : '' ?> value="<?php echo Employe::ENGINEER ?>">Ingenieur</option>
          <option <?php echo ($employe->getType()==Employe::AGENT)  ? 'selected="selected"' : '' ?> value="<?php echo Employe::AGENT?>">Agent</option>
  </select>
    <div class="row">

        <div class="input-append span3 datepicker date" data-date="<?php echo $startDate ?>" data-date-format="yyyy-mm-dd">
            <label>Date de début</label>
            <input class="span2" size="16" type="text" name="start_date" value="<?php echo $startDate ?>">
            <span class="add-on"><i class="icon-th"></i></span>
        </div>

        <div class="input-append span3 datepicker date" data-date="<?php echo $endtDate ?>" data-date-format="yyy-mm-dd">
            <label>Date de fin</label>
            <input class="span2" size="16" type="text" name="end_date" value="<?php echo $endDate ?>">
            <span class="add-on"><i class="icon-th"></i></span>
        </div>

        <div class="span3">
          <br />
          <input type="submit" value="Appliquer le filtre" class="btn btn-success" />
        </div>
    </div>
</form>

<?php if(!empty($guards[Employe::ENGINEER])) :?>
<h2>Gardes des Ingénieurs</h2>
	<table class="table table-striped" width="500" aling="center">
		<thead>
			<th>Date</th><th>Nom et Prénom</th><th>Service</th><th>Adresse</th><th>Téléphone</th>
		</thead>
        <tbody>
        	<?php foreach($guards[Employe::ENGINEER] as $guard):?>
                <tr>
                	<td>
                        <strong><?php echo $guard->unit_type == GuardUnit::WEEKEND ? 'Week-end' : $guard->unit_label ?></strong>
                        <br />
                        Du <?php echo date('d/m/Y', strtotime($guard->start_date)) ?>
                        Au <?php echo date('d/m/Y', strtotime($guard->end_date)) ?>
                    </td>
                    <td><?php echo $guard->first_name.' '.$guard->last_name?></td>
                    <td><?php echo ucfirst($guard->service_name)?></td>
                    <td><?php echo $guard->adresse?></td>
                    <td>Bureau: <?php echo $guard->tele_bureau?><br />GSM: <?php echo $guard->gsm?></td>
                  <td> <?php echo "<a href='form.php?id=".$guard->id."'>modifier</a>" ?>
                </td>
                </tr>
            <?php endforeach ?>
        </tbody>
	</table>
<?php endif ?>

<?php if(!empty($guards[Employe::AGENT])) :?>
<h2>Gardes des agents</h2>
	<table class="table table-striped" width="500" aling="center">
		<thead>
			<th>Date</th><th>Nom et Prénom</th><th>Service</th><th>Adresse</th><th>Téléphone</th>
		</thead>
        <tbody>
        	<?php foreach($guards[Employe::AGENT] as $guard):?>
                <tr>
                	<td>
                        <strong>Semaine</strong>
                        <br />
                        Du <?php echo date('d/m/Y', strtotime($guard->start_date)) ?>
                        Au <?php echo date('d/m/Y', strtotime($guard->end_date)) ?>
                    </td>
                    <td><?php echo $guard->first_name.' '.$guard->last_name?></td>
                    <td><?php echo ucfirst($guard->service_name)?></td>
                    <td><?php echo $guard->adresse?></td>
                    <td>Bureau: <?php echo $guard->tele_bureau?><br />GSM: <?php echo $guard->gsm?></td>
                  <td> <?php echo "<a href='form.php?id=".$guard->id."'>modifier</a>" ?>
                </td>
                </tr>
            <?php endforeach ?>
        </tbody>
	</table>
<?php endif ?>
</div>
</body>
</html>